package com.ceshiren.mini.controller;

import com.ceshiren.mini.dto.PlanDTO;
import com.ceshiren.mini.dto.PlanTestCasesDTO;
import com.ceshiren.mini.dto.RecordDTO;
import com.ceshiren.mini.dto.ReportDTO;
import com.ceshiren.mini.service.PlanService;
import com.ceshiren.mini.service.RecordService;
import com.ceshiren.mini.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static com.ceshiren.mini.util.ResultCode.*;
import static com.ceshiren.mini.util.ResultCode.PARAMETER_NOT_EXIST;

@RestController
@RequestMapping("record")
@Api(tags = "测试记录管理")
public class RecordController {

    private RecordService recordService;

    @Autowired
    public void setRecordService(RecordService recordService) {
        this.recordService = recordService;
    }


    //执行测试
    @PostMapping
    @ApiOperation(value = "测试记录的新增")
    public R createTask(@RequestBody RecordDTO recordDTO) {
        //recordDTO 不能为空
        if (Objects.isNull(recordDTO)) {
            return R.error(PARAMETER_ERROR).message("测试记录为空");
        }
        //id 测试计划id 不能为空
        if (Objects.isNull(recordDTO.getPlan_id())) {
            return R.error(PARAMETER_ERROR).message("测试计划ID不能为空");
        }

        RecordDTO recordDTO1 = recordService.run(recordDTO);

        return null != recordDTO1
                ? R.ok().message(recordDTO1.getId() + "success add")
                : R.error(ADDERROR).message("计划执行失败");

    }

    //测试记录查找
    @ApiOperation(value = "测试记录查找")
    @GetMapping
    public R getPlan(@RequestParam(name = "plan_id", value = "plan_id", required = false) Integer id) {
        if (null == id) {
            //获取所有的测试计划
            List<RecordDTO> recordDTOList = recordService.getRecordList();

            //返回的实体类如果是空，null  error
            return (recordDTOList == null || recordDTOList.isEmpty())
                    ? R.error(PARAMETER_NOT_EXIST).message("没有测试计划")
                    : R.ok().data(recordDTOList);
        } else {
            //根据ID获取测试计划
            RecordDTO recordDTO = new RecordDTO();
            recordDTO.setPlan_id(id);
            System.out.println("RecordDTO:" + recordDTO);
            //RecordDTO recordDTO1 = recordService.getRecordDTO(recordDTO);
            List<RecordDTO> recordDTOList = recordService.getRecordDTOList(recordDTO);
            //返回的实体类如果是空，null  error
            if (recordDTOList.isEmpty())
                return R.error(PARAMETER_NOT_EXIST).message("测试任务不存在");
            return R.ok().data(recordDTOList);
        }
    }

    //更新report报告
    @ApiOperation(value = "allure报告地址更新")
    @GetMapping("report")
    public R updateReport(@RequestParam String build_url) {
        ReportDTO reportDTO = new ReportDTO();
        reportDTO.setBuild_url(build_url);
        int i = recordService.updateReport(reportDTO);
        return i>0
                ? R.ok().message(i + "条allure报告更新成功")
                : R.error(PARAMETER_NOT_EXIST).message("allure报告更新失败");

    }
}
